let OPENID
Page({
  data: {
    list:[],      
    showModalStatus: false       
  },
  onLoad(options) {
    OPENID = options.OpenID
    OPENID = 'oVvRg448uvjAD6uRq1fLeVEq8x6s'
    this.getMyAddress()
  },
  // onShow(){
  //   this.getMyAddress()
  // },
  getMyAddress(){
    wx.cloud.database().collection('User-Address')
    .where({
     _openid:OPENID
    })
    .get()
    .then(res=>{
      console.log(res)
     this.setData({
       list:res.data
     })
    })
  },
  bindDel(e){
    let that =this
    console.log('e.currentTarget.dataset:',e.currentTarget.dataset.bid)
    let del = e.currentTarget.dataset.bid
    wx.showModal({
      title: '提示',
      content: '确定删除此地址',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.cloud.database().collection('User-Address')
          .doc(del)
          .remove({
          })
          .then(res=>{
            wx.showToast({
              title: '删除成功',
            })
            console.log('删除成功:',res)
            that.getMyAddress()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  powerDrawer: function (e) {  
    var currentStatu = e.currentTarget.dataset.statu;  
    this.util(currentStatu)  
  },  
  util: function(currentStatu){  
    /* 动画部分 */  
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({  
      duration: 200,  //动画时长  
      timingFunction: "linear", //线性  
      delay: 0  //0则不延迟  
    });
    
    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;  
  
    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();  
  
    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({  
      animationData: animation.export()  
    })  
      
    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {  
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();  
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({  
        animationData: animation  
      })  
      //关闭  
      if (currentStatu == "close") {  
        this.setData(  
          {  
            showModalStatus: false  
          }  
        );  
      }  
    }.bind(this), 200)  
    // 显示  
    if (currentStatu == "open") {  
      this.setData(  
        {  
          showModalStatus: true  
        }  
      );  
    }  
  },
  loginForm: function(data) {
    console.log(data.detail.value)//  {username: "hgj", password: "fsdfsd"}
    var name = data.detail.value.rName
    var phone = data.detail.value.phone
    var province  = data.detail.value.province
    var city = data.detail.value.city
    var area = data.detail.value.area
    var DetailedAddress = data.detail.value.DetailedAddress
    console.log(name,phone,province,city,area,DetailedAddress)
    if(name!=''&&phone!=''&&province!=''&&city!=''&&area!=''&&DetailedAddress!=''){
      this.addAd(name,phone,province,city,area,DetailedAddress)
    }else{
      wx.showToast({
        title: '请勿留空',
        icon:'error'
      })
    }
  },
  addAd(n,p,pr,c,a,d){
    wx.cloud.database().collection('User-Address')
    .add({
      data:{
        // _openid:OPENID,
        name:n,
        phone:p,
        province:pr,
        city:c,
        area:a,
        DetailedAddress:d
      }
    })
    .then(res=>{
      console.log('地址添加成功:',res)
      this.getMyAddress()
    })
  }
})